import 'dart:convert';
import 'dart:math';

import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/discount_page_item_entity.g.dart';

@JsonSerializable()
class CouponBeanList {
	List<CouponBean>? list = [];
	String? total = '';

	CouponBeanList();

	factory CouponBeanList.fromJson(Map<String, dynamic> json) => $CouponBeanListFromJson(json);

	Map<String, dynamic> toJson() => $CouponBeanListToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}

@JsonSerializable()
class CouponBean {
	int? activityPrice = 0;
	String? beginDate = '';
	bool? canOverlay = false;
	bool? canOverlayOther = false;
	List<DiscountPageItemConditionList>? conditionList = [];
	int? couponAmount = 0;
	String? couponDenomination = '';
	String? couponDesc = '';
	String? couponName = '';
	int? couponNum = 0;// 优惠券数量
	int? couponStatus = 0;
	String? couponType = '';
	String? couponUnit = '';
	int? couponUseNum = 0;// 优惠券使用数量
	int? deductionAmount = 0;
	int? discountAmount = 0;
	int? discountMaxAmount = 0;
	String? endDate = '';
	int? maxQuantity = 0;
	String? mchCouponId = '';
	String? memberCouponId = '';
	int? overlayLimit = 0;// 叠加限制
	int? promotionsMode = 0;
	int? promotionsType = 0;
	int? quantityLimit = 0;
	int? remainDays = 0;
	String? rowNum = '';
	bool? selected = false;
	int? superpositionLimit = 0;//是否和活动互斥 0：不限制 1：限制
	String? thresholdDesc = '';

	CouponBean();

	factory CouponBean.fromJson(Map<String, dynamic> json) => $CouponBeanFromJson(json);

	Map<String, dynamic> toJson() => $CouponBeanToJson(this);

	@override
	String toString() {
		return '[$couponName，codeId:${memberCouponId}，${superpositionLimit == 1}，($couponUseNum/$couponNum)]';
	}

	// 优惠券选中状态
	String get showUnit => couponUnit == '折' ? '折' : '$couponUnit';

	// 判断是否可叠加
	bool get isOverlay {
		return canOverlay == true && couponNum != null && couponNum! > 1;
	}

	// 获取有效期
	String get validDate {
		return '$beginDate 至 $endDate';
	}

	// 获取最大优惠券可用数量
	int maxCouponNum(int maxLimit) {
    return min(maxLimit, overlayLimit!);
  }

  // 判断是否可以叠加
	bool showOverlayText(List<CouponBean>? chooseData) {
		if (chooseData == null || chooseData.isEmpty) {
      return false;
    }
    if (chooseData.contains(this)) {
      return false;
    }
		//
    return overlayLimit != null && overlayLimit! > 0;
	}

	@override
	bool operator ==(Object other) {
		if (identical(this, other)) {
		  return true;
		}
		return other is CouponBean && other.mchCouponId == mchCouponId && other.memberCouponId == memberCouponId;
	}

	// 是否是油品券
	bool get isOilCoupon {
		return couponType == 'energy';
	}

	// 是否是商品券
	bool get isGoodsCoupon {
		return couponType == 'store_goods';
	}


  @override
  int get hashCode => mchCouponId.hashCode;
}

@JsonSerializable()
class DiscountPageItemConditionList {
	String? condition = '';
	String? fieldName = '';
	int? minValue = 0;

	DiscountPageItemConditionList();

	factory DiscountPageItemConditionList.fromJson(Map<String, dynamic> json) => $DiscountPageItemConditionListFromJson(json);

	Map<String, dynamic> toJson() => $DiscountPageItemConditionListToJson(this);

	@override
	String toString() {
		return jsonEncode(this);
	}
}